package com.darknight.ftp.component.controller;

import com.darknight.ftp.component.service.UploadFileService;
import com.darknight.ftp.component.service.impl.DefaultUploadFileManager;
import com.darknight.ftp.component.service.impl.UploadFileManager;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import javax.annotation.Resource;

/**
 * Created by DarKnight on 2017/7/13.
 */
@Controller("transferController")
@RequestMapping(value = "easyUpload")
public class TransferController {
    private final String storeFilePath = "easyUpload/default";
    private String uploadPageBean;
    private UploadFileManager uploadFileService;
    private UploadFileService defaultUploadFileService;

    @Value("${ftpserver.uploadPageBean}")
    public void setUploadPageBean(String uploadPageBean) {
        this.uploadPageBean = uploadPageBean;
    }

    @Resource(name = "defaultUploadFileManager")
    public void setUploadFileService(UploadFileManager uploadFileService) {
        this.uploadFileService = uploadFileService;
    }

    @Resource
    public void setDefaultUploadFileService(UploadFileService defaultUploadFileService) {
        this.defaultUploadFileService = defaultUploadFileService;
    }

    //    @RequestMapping(value = {"uploadFile"}, method = {RequestMethod.POST})
//    public String uploadFile(@RequestParam("uploadFile")MultipartFile uploadFile, Model model) throws IOException {
//        boolean uploadResult = uploadFileService.uploadFile(uploadFile, storeFilePath);
//
//        if(uploadResult) {
//            model.addAttribute("uploadFile", uploadFile);
//        }
//
//        return uploadPageBean;
//    }

    @RequestMapping(value = {"uploadFile"}, method = {RequestMethod.POST})
    public String uploadFile(@RequestParam("uploadFile")MultipartFile uploadFile, Model model) throws IOException {
        boolean uploadResult = defaultUploadFileService.uploadFile("admin", "admin", uploadFile, storeFilePath);

        if(uploadResult) {
            model.addAttribute("uploadFile", uploadFile);
        }

        return uploadPageBean;
    }
}
